<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >

      <div class="title-container">
        <h3 class="title">一般贸易后台管理</h3>
      </div>
      <div style="margin-bottom:10px">
        <el-radio v-model="loginForm.type" :label="0">账号密码登录</el-radio>
        <!-- <el-radio v-model="loginForm.type" :label="1">验证码登录</el-radio> -->
      </div>
      <el-form-item prop="area_code">
        <span class="svg-container">
          <i
            class="el-icon-location-information"
          />
        </span>
        <el-select v-model="loginForm.area_code" style="width:90%" placeholder="请选择区号">
          <el-option
            v-for="item in AreaCodeList"
            :key="item.area_code"
            :label="'+'+ item.area_code + item.area_name"
            :value="item.area_code"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="username">
        <span class="svg-container">
          <i class="el-icon-s-custom" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.mobile"
          placeholder="手机号"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-form-item v-if="loginForm.type == 0" prop="password">
        <span class="svg-container">
          <i class="el-icon-lock" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          autocomplete="on"
          @blur="capsTooltip = false"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <i
            :class="passwordType === 'password' ? 'el-icon-view' : 'el-icon-minus'"
          />
        </span>
      </el-form-item>
      <el-form-item v-if="loginForm.type == 1" prop="code">
        <el-row>
          <el-col :span="18">
            <el-input
              ref="code"
              v-model="loginForm.code"
              class="input"
              name="code"
              placeholder="请输入验证码"
              auto-complete="on"
              style="width:100%"
            />
          </el-col>
          <el-col :span="6" style="height:52px; align:right">
            <el-button
              :disabled="time != '获取验证码'"
              type="primary"
              size="medium"
              style="width:100%;position:relative;top:8px;right:10px;text-align:center"
              @click.native.prevent="getCode"
            >{{ time }}</el-button>
          </el-col>
        </el-row>

      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        size="medium"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登 录</el-button>
    </el-form>

  </div>
</template>

<script>
import { listMobileAreaCode, sendVerifyCode } from '@/api/user'

export default {
  name: 'Login',
  components: {},
  data() {
    return {
      time: '获取验证码',
      AreaCodeList: [],
      loginForm: {
        mobile: '',
        password: '',
        area_code: 86,
        type: 0,
        code: ''
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: 'dashboard',
      otherQuery: {},
      timer: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  async created() {

  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
    this.getListMobileAreaCode()
  },
  methods: {
    getListMobileAreaCode() {
      listMobileAreaCode().then(res => {
        this.AreaCodeList = res.data.list
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getCode() {
      if (this.timer !== null) return
      sendVerifyCode({
        area_code: this.loginForm.area_code,
        mobile: this.loginForm.mobile,
        flag: 2
      }).then(res => {
        this.$message.success('发送成功')
        this.time = 119
        this.timer = window.setInterval(() => {
          this.time--
          if (this.time <= 0) {
            if (this.timer) window.clearInterval(this.timer)
            this.time = '获取验证码'
            this.timer = null
          }
        }, 1000)
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(async() => {
              this.$router.push({ path: '/' })
              this.loading = false
            })
            .catch(() => {
              this.loginForm.code = ''
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.el-select .el-input{
width: 100% !important;

}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 11px;
    font-size: 16px;
    color: #fff;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
